package cn.fl.dk.web.service.impl;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.dk.facade.api.IDkBillInfoApiService;
import cn.fl.dk.facade.dto.DkBillInfoDTO;
import cn.fl.dk.web.service.IDkBillInfoService;
import cn.fl.dk.web.transform.DkBillInfoVOTransform;
import cn.fl.dk.web.vo.DkBillInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description: 垫款单(DK_BILL_INFO) 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class DkBillInfoServiceImpl implements IDkBillInfoService {

	/**
	 * 日志
	 */
	//private static final Logger logger = LoggerFactory.getLogger(DkBillInfoServiceImpl.class);
	
	/**
	 * 自定义 API 自行注入需要的API服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;

	@Autowired
	private IDkBillInfoApiService dkBillInfoApiService;

	@Override
	public DkBillInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newLogger("主键参数不能为空!").setKeys("selectById", CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "获取对象主键不能为空!");
		}
		//TODO 业务逻辑
		DkBillInfoDTO dto = dkBillInfoApiService.selectDkBillInfoById(id);//this.自定义ApiService.selectDkBillInfoDTOById(id);
		return DkBillInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(DkBillInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("新增对象不存在!").setKeys("add", CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"新增对象不存在!");
		}
		return this.dkBillInfoApiService.addDkBillInfo(DkBillInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newLogger("删除对象主键不能为空!").setKeys("removeById", CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		}
		//return this.dkBillInfoApiService.removeDkBillInfoDTOById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.newLogger("删除主键参数集合不能为空!").setKeys("removeByIds", CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"删除对象主键不能为空!");
		}
		return false;
    }

	@Override
	public boolean modify(DkBillInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newLogger("修改对象不能为空!").setKeys("modify", CurrentThreadContext.getCurrentUserName()).info();
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"修改对象不能为空!");
		}
		boolean flag = true;
		try{
			dkBillInfoApiService.modifyDkBillInfo(DkBillInfoVOTransform.toDTO(entity));
		}catch (BaseException e) {
			flag = false;
			BaseLogUtils.newLogger("修改异常!").setKeys("modify", CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, BaseExceptionConstant.BASE_EXCEPTION_CODE_603_NAME);
		} catch(Exception e){
			flag = false;
			BaseLogUtils.newLogger("修改异常!").setKeys("modify", CurrentThreadContext.getCurrentUserName()).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, BaseExceptionConstant.BASE_EXCEPTION_CODE_603_NAME);
		}
		return flag;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		return this.dkBillInfoApiService.searchDkBillInfoListPage(page);
		//TODO 业务逻辑
	}

	@Override
	public DkBillInfoDTO queryBillInfoByRuleId(Long ruleId)
			throws BaseException {
		return this.dkBillInfoApiService.queryBillInfoByRuleId(ruleId);
	}
}
